package com.itheima.smspringboot.service;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.smspringboot.entity.SchoolClass;
import com.itheima.smspringboot.entity.Student;
import com.itheima.smspringboot.exception.CustomerExpception;
import com.itheima.smspringboot.mapper.SchoolClassMapper;
import com.itheima.smspringboot.mapper.StudentMapper;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {

    @Resource
    private  StudentMapper studentMapper;
    @Autowired
    private SchoolClassMapper schoolClassMapper;

    public  void deleteBatch(List<Student> studentList) {
        for (Student student : studentList) {
            studentMapper.deleteById(student.getStuId());
        }
    }

    public  void deleteById(Integer stuId) {
        SchoolClass schoolClass = schoolClassMapper.selectById(stuId);
        if (schoolClass != null) {
            schoolClass.setMonitor(null);
            schoolClassMapper.updateById(schoolClass);
        }
        studentMapper.deleteById(stuId);

    }

    public  void updateById(Student student) {
        studentMapper.updateById(student);
    }

    public List<Student> selectAll(Student student) {
        return studentMapper.selectAll(student);
    }



    public PageInfo selectPage(Integer pageNum, Integer pageSize, Student student) {
        //H=开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        List<Student> list = studentMapper.selectAll(student);
        return PageInfo.of(list);

    }

    public void add(Student student) {
        studentMapper.insert(student);
    }

    /*public void add(Student student) {

        Student dbstudent = studentMapper.selectByUsername(student.getUsername());
        if (dbstudent != null) {
            throw new CustomerExpception("账号重复");
        }

        //如果密码为空，默认密码为student
        if (StrUtil.isBlankIfStr(student.getPassword())) {
            student.setPassword("student");
        }

        studentMapper.insert(student);
    }

    public void updateById(Student student) {
        studentMapper.updateById(student);
    }

    public void deleteById(Integer id) {
        studentMapper.deleteById(id);
    }

    public void deleteBatch(List<Student> studentList) {
        for (Student student : studentList) {
            this.deleteById(student.getId());
        }
    }*/




}
